package com.example.guiguiapplicatio.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.guiguiapplicatio.api.GetCountTotalApi;
import com.example.guiguiapplicatio.api.Interface.AllCallback;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.databinding.FragmentEidtBinding;
import com.example.guiguiapplicatio.util.ResponseBody;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


public class EditFragment extends Fragment implements AllCallback {
    FragmentEidtBinding binding;
    private GetCountTotalApi getCountTotalApi;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentEidtBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        getCountTotalApi = new GetCountTotalApi();
        getCountTotalApi.setAllCallback(this);
        getCountTotalApi.performGetCountTotal();
        return view;
    }

    @Override
    public void onResume() {
        BottomNavigationView bottomNavigationView = requireActivity().findViewById(R.id.nav_view);
        bottomNavigationView.setVisibility(View.VISIBLE);
        super.onResume();
    }

    @Override
    public void onSuccess(ResponseBody<Object> responseBody) {
        if (responseBody != null && responseBody.getData() != null) {
            JsonObject jsonObject = JsonParser.parseString(responseBody.getData().toString()).getAsJsonObject();
            int recordTotal = jsonObject.get("recordTotal").getAsInt();
            int toBeSigned = jsonObject.get("toBeSigned").getAsInt();
            int signed = jsonObject.get("signed").getAsInt();
            int signingExpiration = jsonObject.get("signingExpiration").getAsInt();
            int followUpTotal = jsonObject.get("followUpTotal").getAsInt();
            if (getActivity()!=null){
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        binding.jiandangNum.setText(String.valueOf(recordTotal));
                        binding.xvyueNum.setText(String.valueOf(toBeSigned));
                        binding.qianyueNum.setText(String.valueOf(signed));
                        binding.guoqiNum.setText(String.valueOf(signingExpiration));
                        binding.suifangNum.setText(String.valueOf(followUpTotal));
                    }
                });
            }

        } else {
            Log.d("info", "responesdBody" + responseBody.toString());
        }

    }

    @Override
    public void onFailure(String errorMessage) {

        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getContext(), "数据统计相应失败", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
